package com.allqj.sfx.ddd.member.application.service;

import com.allqj.sfx.application.TableHeaderApplicationService;
import com.allqj.sfx.application.dto.DictDTO;
import com.allqj.sfx.application.dto.TableColumnDTO;
import com.allqj.sfx.ddd.member.application.assembler.DictAssember;
import com.allqj.sfx.ddd.member.application.dto.AdminUserLoginDTO;
import com.allqj.sfx.ddd.member.domain.entity.admin.AdminUser;
import com.allqj.sfx.ddd.member.domain.entity.admin.Tenant;
import com.allqj.sfx.ddd.member.domain.enums.BusinessExceptionEnum;
import com.allqj.sfx.ddd.member.domain.service.AdminUserService;
import com.allqj.sfx.domain.entity.UserInfo;
import com.allqj.sfx.domain.enums.DomainTypeEnum;
import com.allqj.sfx.domain.enums.PageEnum;
import com.allqj.sfx.domain.service.AuthenticationService;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author sjw
 * @className UserApplicationService
 * @date 2022/7/20 9:38
 * @description: 业务编排
 * 依赖domain层的service&repository
 * 会员管理-管理端-用户业务：登录、注册等
 */

@Component
public class AdminUserApplicationService {
    private final AdminUserService adminUserService;
    private final AuthenticationService authenticationService;
    private final TableHeaderApplicationService tableHeaderApplicationService;

    public AdminUserApplicationService(AdminUserService adminUserService,
                                       AuthenticationService authenticationService,
                                       TableHeaderApplicationService tableHeaderApplicationService) {
        this.adminUserService = adminUserService;
        this.authenticationService = authenticationService;
        this.tableHeaderApplicationService = tableHeaderApplicationService;
    }

    /**
     * 登录
     *
     * @param loginDTO
     * @return
     */
    public String login(AdminUserLoginDTO loginDTO) {
        // 登录
        AdminUser adminUser = adminUserService.login(loginDTO.getAccount(), loginDTO.getPassword());
        if (adminUser == null) BusinessExceptionEnum.ACCOUNT_NOT_EXIST.throwException();
        // 获取&返回token
        UserInfo userInfo = new UserInfo();
        userInfo.setUserId(adminUser.getId());
        userInfo.setUserName(adminUser.getUserName());
        userInfo.setTenantId(adminUser.getTenantId());
        userInfo.setDeptId(adminUser.getDeptId());
        userInfo.setRoleId(adminUser.getRoleId());
        userInfo.setDomainTypeEnum(DomainTypeEnum.MEMBER_ADMIN);
        return authenticationService.token(userInfo);
    }

    /**
     * 用户管理-列表表头
     *
     * @return
     */
    public List<TableColumnDTO> pageHeader() {
        List<TableColumnDTO> headers = tableHeaderApplicationService.header(PageEnum.MEMBER_ADMIN_USER);
        return Optional.ofNullable(headers).orElse(new ArrayList<>());
    }

    /**
     * 单位字典
     * @return
     */
    public List<DictDTO> agency() {
        List<Tenant> tenants = adminUserService.agency();
        if (tenants == null) return null;
        return tenants.stream().map(DictAssember.INSTANCE::dto2Entity).collect(Collectors.toList());
    }
}
